package com.cyw.common.config;

import com.cyw.common.annotations.NotOnProduction;
import com.cyw.common.domain.enums.RuntimeMode;
import com.cyw.common.utils.BaseUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

/**
 * @Author:cyw
 * @CreateTime: 2021/3/11 10:03
 **/
@Configuration
public class ApplicationConfig {

    @Bean
    public BeanDefinitionRegistry beanDefinitionRegistry(
            ConfigurableApplicationContext ctx,
            // TODO:  获取运行环境再更改
            @Value("${spring.cloud.nacos.config.namespace:local}") String env
    ) {
        BeanDefinitionRegistry registry = (BeanDefinitionRegistry) ctx.getAutowireCapableBeanFactory();

        RuntimeMode mode = RuntimeMode.nameOf(env.toLowerCase());

        if (BaseUtils.isNotSame(RuntimeMode.PRODUCTION, mode)) {
            return registry;
        }

        Map<String, Object> withAnnotation = ctx.getBeansWithAnnotation(NotOnProduction.class);
        for (String beanName : withAnnotation.keySet()) {
            registry.removeBeanDefinition(beanName);
        }
        return registry;
    }

}

    
    
    
    
    
    
    
    
    

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    